<template>
  <div class="articleDetail center-wrapper">
    <div class="breadcrum">
      <div>当前位置：<span @click="goArticleList">文章</span> > <span>文章详情</span></div>
    </div>
    <div class="article-content">
      <section class="article-info">
        <p class="article-title">{{ receptData?.title }}</p>
        <div class="article-links">
          <div class="link-item flex-c-c">
            <img :src="loadAssetsURL('article/clock.png')" alt="时间" />
            {{ receptData.create_time }}
          </div>
          <div class="link-item flex-c-c">
            <img
              :src="loadAssetsURL('article/like.png')"
              alt="点赞"
              v-if="receptData.like_state == '1'"
            />
            <img :src="loadAssetsURL('article/isLike.png')" alt="点赞" v-else />
            <p
              class="link-count"
              :style="{ color: receptData.like_state == '1' ? '#000' : '#0075ff' }"
            >
              {{ receptData.likes ? receptData.likes : 0 }}
            </p>
          </div>
          <div class="link-item flex-c-c">
            <img :src="loadAssetsURL('article/look.png')" alt="查看" />
            {{ receptData.views ? receptData.views : 0 }}
          </div>
          <div class="link-item flex-c-c">
            <img :src="loadAssetsURL('article/message.png')" alt="回复" />
            {{ receptData.comments ? receptData.comments : 0 }}
          </div>
          <div class="link-item flex-c-c">
            作者：{{ receptData?.nickname ? receptData?.nickname : '无名' }}
          </div>
          <div
            class="link-item flex-c-c author-delete"
            v-if="!receptData?.user_state"
            @click="openMyDialog"
          >
            删除
          </div>
          <p @click="goEdit" v-if="!receptData?.user_state">编辑</p>
        </div>
        <div class="profile">{{ receptData.introduction }}</div>
        <div class="article-main">
          <mavon-editor
            class="article-main-editor"
            ref="showMarkDown"
            v-model="receptData.file_url"
            :subfield="false"
            :toolbarsFlag="false"
            defaultOpen="preview"
            :editable="false"
          />
        </div>
        <div class="article-labels">
          <img :src="loadAssetsURL('article/label.png')" alt="标签" />
          <p class="label-item" v-for="(labelItem, lIndex) in receptData.label" :key="lIndex">
            {{ labelItem }}
          </p>
        </div>
      </section>

      <section class="article-profile" v-if="!route.query.isQuality">
        <div class="reply-head flex-b-c">
          <p
            class="flex-c-c"
            :class="{ isChosed: currentContent == 2 }"
            @click="currentContent = 2"
          >
            目录
          </p>
          <p
            class="flex-c-c"
            :class="{ isChosed: currentContent == 1 }"
            @click="currentContent = 1"
          >
            评论
          </p>
        </div>
        <div class="article-desc" v-if="currentContent == 1">
          <articleProfile :profile="receptData" @like="doLikes" @refresh="refresh">
            <template #head>
              <p class="profile-title">{{ receptData.title }}</p>
            </template>
          </articleProfile>
        </div>

        <div class="tocs" v-if="currentContent == 2">
          <p class="tocs-title">{{ currentSrcollId ? '导航目录' : '暂无目录' }}</p>
          <div class="tocsMain" v-if="currentSrcollId">
            <div
              class="tocsItem flex-c"
              v-for="(tItem, tIndex) in tocs"
              :key="tIndex"
              :class="{ currentScroll: currentSrcollId == tItem.id }"
            >
              <div class="tocs-icon"></div>
              <p @click="scrollDom(tItem)" class="t-of-1">
                {{ tItem.name }}
              </p>
            </div>
          </div>
        </div>
      </section>

      <section class="reply-detail" v-else>
        <div class="reply-head flex-b-c">
          <p>评论</p>
          <img :src="loadAssetsURL('article/close.png')" alt="返回" @click="goArticleList" />
        </div>
        <div class="reply-main">
          <div class="main-content">
            <articleProfile :profile="profileData" :showAll="true" @refresh="refresh">
              <template #default>
                <div class="main-margin"></div>
              </template>
            </articleProfile>
          </div>
        </div>
      </section>
    </div>

    <my-dialog ref="dialogRef" :dialog-class="'deleteArticleDialog'">
      <template #header>
        <p class="dialogTitle flex-c-c">删除</p>
      </template>
      <template #default>
        <p class="dialogMain">确认删除{{ receptData?.title }}？</p>
      </template>
      <template #footer>
        <div class="footer flex-b-c">
          <p @click="fileDel">确认</p>
          <p @click="closeMyDialog">取消</p>
        </div>
      </template>
    </my-dialog>
  </div>
</template>

<script setup lang="ts">
import { loadAssetsURL } from '@/utils/load-assets'
import articleProfile from './components/article-profile/Article-Profile.vue'
import { nextTick, ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { displayApi, fileLikeApi, fileDelApi } from '@/service/api'
import { iscode } from '@/utils'
import useProfileStore from '@/stores/profile/profile'
import { onMounted } from 'vue'
import MyDialog from '@/components/dialog/MyDialog.vue'
import { onUnmounted } from 'vue'
const dialogRef = ref<any>()
const router = useRouter()
const route = useRoute()
const profileStore = useProfileStore()
//右边回复
const receptData = ref<any>({})

const profileData = ref<any>({})

const display_state = ref<number>(0)

const showMarkDown = ref<any>()

const goArticleList = () => {
  router.go(-1)
}

//获取文章详情
const displayArticle = async () => {
  if (route.query.articleId) {
    let res = await displayApi({
      file_id: route.query.articleId,
      display_state: display_state.value
    })
    if (iscode(res)) {
      receptData.value = res.result
      receptData.value.file_id = route.query.articleId
      for (const key in receptData.value.list) {
        if (key == route.query?.isQuality) {
          profileData.value.list = [receptData.value.list[key]]
        }
      }
      nextTick(() => {
        getTocs()
      })
    }
  }
}

const tocs = ref<any>([])
const currentContent = ref<number>(1)
const getTocs = () => {
  if (!tocs.value.length) {
    const myData = showMarkDown.value.d_render
    const reg = /a id="((\w|-|\s)+)/gi

    myData.replace(reg, (item: any) => {
      let id = item.substr(6)
      let name: any = document.getElementById(id)?.parentNode
      tocs.value.push({ id: id, name: name.innerText })
    })
    currentSrcollId.value = tocs.value[0]?.id
  }
}

const scrollDom = (item: any) => {
  document.getElementById(item.id)?.scrollIntoView()
  currentSrcollId.value = item.id
}

//点赞
const doLikes = async () => {
  display_state.value = 1
  let res = await fileLikeApi({ id: route.query.articleId })
  if (iscode(res)) {
    displayArticle()
  }
}

//去编辑
const goEdit = () => {
  profileStore.isEditArticle = true
  router.push({
    path: '/profile',
    query: {
      articleId: route.query.articleId
    }
  })
}

const refresh = () => {
  display_state.value = 1
  displayArticle()
}

//删除文章
const fileDel = async () => {
  let res = await fileDelApi({ id: route.query.articleId })
  if (iscode(res, true)) {
    router.go(-1)
  }
}

//打开弹窗
const openMyDialog = () => {
  dialogRef.value.openDialog()
}

//取消删除
const closeMyDialog = () => {
  dialogRef.value.closeDialog()
}
const init = () => {
  displayArticle()
}

const currentSrcollId = ref<any>()
const getCurrentScroll = () => {
  let scrollItems = document.getElementsByTagName('a')
  let curTop = document.documentElement.scrollTop || document.body.scrollTop
  let itemList: any = []

  for (let i = scrollItems.length - 1; i >= 0; i--) {
    if (scrollItems[i].id) {
      if (!itemList.includes(scrollItems[i].id)) {
        itemList.unshift(scrollItems[i].id)
      }
    }
  }

  for (let i = itemList.length - 1; i >= 0; i--) {
    let dom: any = document.getElementById(itemList[i])
    if (dom.offsetTop + 450 < curTop) {
      currentSrcollId.value = dom.id
      break
    }
  }
}
onMounted(() => {
  watch(
    () => route.query,
    (newVal, oldVal) => {
      if (newVal) {
        displayArticle()
      }
    }
  )
  window.addEventListener('scroll', getCurrentScroll)
})

onUnmounted(() => {
  window.removeEventListener('scroll', getCurrentScroll)
})

init()
</script>

<style lang="less" scoped>
.articleDetail {
  margin: 53px auto;
  .breadcrum {
    font-size: 14px;
    font-weight: 500;
    color: #999999;
    user-select: none;
    span {
      color: #0075ff;
    }
    span:first-child {
      color: #999999;
      cursor: pointer;
    }
  }
  .article-content {
    margin-top: 25px;
    display: flex;
    justify-content: space-between;
    .article-info {
      width: 1057px;
      height: -webkit-fill-available;
      background: #ffffff;
      box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.05);
      border-radius: 7px 7px 7px 7px;
      padding: 45px 55px;
      .article-title {
        font-size: 37px;
        font-weight: 600;
        color: #252525;
        line-height: 59px;
      }
      .article-links {
        user-select: none;
        display: flex;
        align-items: center;
        margin-top: 25px;
        .link-item {
          font-size: 19px;
          font-weight: 500;
          color: #666666;
          margin-right: 25px;
          img {
            width: 21px;
            height: 21px;
            margin-right: 10px;
          }
          .link-count {
            cursor: auto;
          }
        }
        .author {
          color: #0075ff;
        }
        .author-delete {
          color: #ff003d;
          cursor: pointer;
        }
        p {
          cursor: pointer;
          color: #0075ff;
          margin-left: auto;
        }
      }
      .profile {
        margin-top: 30px;
        background: #f4f4f4;
        border-radius: 7px 7px 7px 7px;
        font-size: 19px;
        font-weight: 500;
        color: #252525;
        line-height: 34px;
        padding: 27px;
      }
      .article-main {
        margin-top: 30px;

        .article-main-editor {
          z-index: 1499;
        }
      }
      .article-labels {
        user-select: none;
        margin-top: 45px;
        display: flex;
        align-items: center;
        img {
          width: 25px;
          height: 25px;
          margin-right: 20px;
        }
        .label-item {
          cursor: pointer;
          margin-right: 30px;
          font-weight: 500;
          color: #0075ff;
          font-size: 17px;
        }
      }
    }
    .article-profile {
      background: #ffffff;
      height: fit-content;
      width: 503px;
      position: sticky;
      top: 150px;

      .reply-head {
        padding: 17px 30px;
        border-bottom: 1px solid #dddddd;

        p {
          cursor: pointer;
          width: 148px;
          height: 52px;
          border-radius: 7px 7px 7px 7px;
          font-size: 20px;
          font-weight: 500;
          color: #000000;
        }
        img {
          cursor: pointer;
          width: 20px;
        }
        .isChosed {
          background: rgba(0, 117, 255, 0.08);
          color: #0075ff;
        }
      }
      .profile-title {
        padding: 30px 20px 36px 22px;
        font-size: 23px;
        font-weight: 600;
        color: #252525;
        line-height: 34px;
      }
      .tocs {
        padding: 40px 56px;
        .tocs-title {
          font-size: 22px;
        }
        .tocsMain {
          margin-top: 30px;
          .tocsItem {
            margin-bottom: 25px;
            .tocs-icon {
              width: 9px;
              height: 9px;
              background: #d9d9d9;
              margin-right: 12px;
            }
            p {
              user-select: none;
              cursor: pointer;
              font-size: 19px;
              font-weight: 500;
              color: #252525;
            }
          }
          .currentScroll {
            .tocs-icon {
              background: #0075ff;
            }
            p {
              color: #0075ff;
            }
          }
        }
      }
    }

    .reply-detail {
      background: #ffffff;
      box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.05);
      border-radius: 7px 7px 7px 7px;
      width: 503px;
      padding: 30px 0px;
      position: sticky;
      top: 150px;
      height: fit-content;
      .reply-head {
        padding: 0 30px 17px 30px;
        border-bottom: 1px solid #dddddd;

        p {
          cursor: pointer;
          // width: 148px;
          // height: 52px;
          // border-radius: 7px 7px 7px 7px;
          font-size: 20px;
          font-weight: 500;
          color: #000000;
        }
        img {
          cursor: pointer;
          width: 20px;
        }
        .isChosed {
          background: rgba(0, 117, 255, 0.08);
          color: #0075ff;
        }
      }
      .reply-main {
        .main-margin {
          margin-bottom: 31px;
        }
      }
      .comment {
        margin-top: 20px;
        font-weight: 500;
        color: #333333;
        line-height: 37px;
        font-size: 17px;
      }
      .reply-bottom {
        margin-top: 16px;
        .reply-date {
          font-size: 16px;
          font-weight: 400;
          color: #666666;
        }
        .replayIcon {
          cursor: pointer;
          display: flex;
          align-items: center;
          img {
            width: 20px;
            margin-right: 5px;
          }
          p {
            color: #0075ff;
          }
        }
      }
    }
  }
  .article-main-editor {
    box-shadow: none !important;
  }
}
</style>

<style lang="less">
.deleteArticleDialog {
  .dialogTitle {
    font-weight: 600;
    color: #252525;
    font-size: 28px;
  }
  .deleteArticleDialog-container {
    padding: 30px 50px !important;
    min-height: auto !important;
  }
  .dialogMain {
    margin: 40px 0;
    font-weight: 500;
    color: #252525;
    font-size: 20px;
  }
  .footer {
    font-weight: 500;
    color: #252525;
    font-size: 20px;
    p {
      cursor: pointer;
    }
    p:first-child {
      color: #0075ff;
    }
  }
}
</style>
